package com.qf.bank.dao.ipml;/*
 * @author     : XXX
 * @date       : 2022/5/4 14:53
 * @description: some description
 账户数据访问层接口实现类
 */

import com.qf.bank.dao.AccountDao;
import com.qf.bank.entity.Account;
import com.qf.bank.util.DBUtils;

import java.math.BigDecimal;
import java.sql.*;
import java.util.List;

public class AccountDaoImpl implements AccountDao {

    @Override
    public void insert(Account account) {

    }

    @Override
    public void updateSave(String cardNo, BigDecimal money) {
        Connection connection = null;
        PreparedStatement ps = null;

        try {
            connection = DBUtils.getConnection();
            String sql = "UPDATE `account` SET money = money+? WHERE cardNo=?";
            ps = connection.prepareStatement(sql);
            ps.setBigDecimal(1, money);
            ps.setString(2, cardNo);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.closeAll(null, ps, connection);
        }
    }

    @Override
    public void updateTake(String cardNo, BigDecimal money) {
        PreparedStatement ps = null;
        Connection connection = null;
        try {
            connection = DBUtils.getConnection();
            String sql = "UPDATE `account` SET money = money-? WHERE cardNo=?";
            ps = connection.prepareStatement(sql);
            ps.setBigDecimal(1, money);
            ps.setString(2, cardNo);
            ps.executeUpdate();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.closeAll(null, ps, connection);
        }
    }

    @Override
    public void update(Account account) {

    }

    @Override
    public void delete(String cardNo) {

    }

    @Override
    public List<Account> selectAll() {
        return null;
    }

    @Override
    public BigDecimal selectMoney(String cardNo) {
        ResultSet resultSet = null;
        PreparedStatement ps = null;
        Connection connection = null;
        try {
            connection = DBUtils.getConnection();
            String sql = "SELECT * FROM account WHERE cardNo=? ";
            ps = connection.prepareStatement(sql);
            ps.setString(1, cardNo);
            resultSet = ps.executeQuery();
            if (resultSet.next()) {
                return resultSet.getBigDecimal("money");
            }
            return new BigDecimal(0);

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.closeAll(resultSet, ps, connection);
        }
    }



    @Override
    public Account select(String cardNo, String passwrod) {

        try {
            Connection connection = DBUtils.getConnection();
            String sql= "SELECT * FROM account WHERE cardNo=? AND PASSWORD=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,cardNo);
            ps.setString(2,passwrod);
            ResultSet resultSet = ps.executeQuery();


            Account account=null;
            if (resultSet.next()) {
                int id = resultSet.getInt("id");
                String cardNo1 = resultSet.getString("cardNo");
                String password = resultSet.getString("password");
                BigDecimal money = resultSet.getBigDecimal("money");
                String name = resultSet.getString("name");
                String address = resultSet.getString("address");
                String phone = resultSet.getString("phone");

                //封装实体
                account=new Account(id,cardNo1,password,money,name,address,phone);
            }

            return account;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            ResultSet resultSet = null;
            Statement ps = null;
            Connection connection=null;
            DBUtils.closeAll(resultSet,ps,connection);
        }

    }

    @Override
    public boolean selectByCardNo(String cardNo) {
        ResultSet resultSet = null;
        PreparedStatement ps = null;
        Connection connection=null;
        try {
            connection = DBUtils.getConnection();
            String sql= "SELECT * FROM account WHERE cardNo=? ";
            ps = connection.prepareStatement(sql);
            ps.setString(1,cardNo);
            resultSet = ps.executeQuery();
            if (resultSet.next()) {
                return true;
            }
            return false;

        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            DBUtils.closeAll(resultSet,ps,connection);
        }
    }
}
